package com.qianxx.viewlibrary.text.line;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.qianxx.viewlibrary.R;

import java.util.List;

public class DoubleLine extends View {
    private Paint mPaint;
    private Resources res;
    private DisplayMetrics dm;
    private boolean isMeasure = true;
    //设置点的半径
    private static final int RADIUS = 5;
    //上折线所有坐标点
    private Point[] mPoints1;
    //下折线所有坐标点
    private Point[] mPoints2;
    //控件的高度
    private int canvasHeight;
    //控件的宽度
    private int canvasWidth;
    // 所有纵坐标值
    private List<Integer> yRawData1;
    // 上线横坐标值集合
    private List<Integer> yRawData2;
    // 下线横坐标值集合
    private List<String> xRawDatas;
    //每个横坐标点的间距
    private static final int MARGIN = 40;
    //横坐标的偏移量
    private static final int START_MARGIN = 10;
    //这是一个阈值用来计算坐标点的y坐标
    private double maxValue;
    //控件内容的长度
    private int tatolWidth;
    private boolean isFirstScrollToMedia = true;
    private int lineColor = R.color.blue;
    private int upCircle = R.drawable.public_fuhao;
    private int downCircle = R.drawable.public_fuhao;

    public DoubleLine(Context context) {
        this(context, null);
    }

    public DoubleLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.line);
        //lineColor = a.getColor(R.styleable.line_line_color, getResources().getColor(R.color.blue));
        upCircle = a.getResourceId(R.styleable.line_up_circle, R.drawable.public_fuhao);
        downCircle = a.getResourceId(R.styleable.line_down_circle, R.drawable.public_fuhao);
        a.recycle();
        initView();
    }

    private void initView() {
        this.res = getContext().getResources();
        this.mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        this.mPaint.setStrokeWidth(2);
        dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        //只计算一次控件的宽高
        if (isMeasure) {
            //获得控件的高度
            this.canvasHeight = getHeight();
            //获得控件的宽度
            this.canvasWidth = getWidth();
            isMeasure = false;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPoints1 = getPoints1();
        mPoints2 = getPoints2();
        drawX(canvas);
        drawUpLine(canvas);
        drawDownLine(canvas);
        drawUpCircle(canvas);
        drawDownCircle(canvas);
        drawDottedLine(canvas);
        if (isFirstScrollToMedia) {
            scrollTMiddle();
            isFirstScrollToMedia = false;
        }
    }

    /**
     * 画上折线的坐标点
     *
     * @param canvas
     */
    private void drawUpCircle(Canvas canvas) {
        mPaint.setColor(res.getColor(lineColor));
        mPaint.setStyle(Style.FILL);
        for (int i = 0; i < mPoints1.length; i++) {
            //canvas.drawCircle(mPoints[i].x, mPoints[i].y, RADIUS, mPaint);
            drawBitmap(upCircle, i * dip2px(MARGIN), mPoints1[i].y - dip2px(10), canvas, mPaint);
        }
    }

    /**
     * 画下折线的坐标点
     *
     * @param canvas
     */
    private void drawDownCircle(Canvas canvas) {
        mPaint.setColor(res.getColor(lineColor));
        mPaint.setStyle(Style.FILL);
        for (int i = 0; i < mPoints2.length; i++) {
            drawBitmap(downCircle, i * dip2px(MARGIN), mPoints2[i].y - dip2px(10), canvas, mPaint);
        }
    }

    /**
     * 画虚线
     *
     * @param canvas
     */
    private void drawDottedLine(Canvas canvas) {
        Paint p = new Paint();
        p.setStyle(Style.STROKE);
        p.setColor(res.getColor(lineColor));
        p.setStrokeWidth(2);
        PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 2);
        p.setPathEffect(effects);
        for (int i = 0; i < mPoints1.length; i++) {
            Path path = new Path();
            path.moveTo(mPoints1[i].x, mPoints1[i].y + dip2px(START_MARGIN));
            path.lineTo(mPoints2[i].x, mPoints2[i].y - dip2px(START_MARGIN + 2));
            canvas.drawPath(path, p);
        }
    }


    /**
     * 画横坐标和横坐标上的点及数值
     *
     * @param canvas
     */
    private void drawX(Canvas canvas) {
        mPaint.setColor(res.getColor(lineColor));
        //画横坐标那条线
        canvas.drawLine(0, canvasHeight - dip2px(20), mPoints1.length * dip2px(MARGIN), canvasHeight - dip2px(20), mPaint);
        tatolWidth = mPoints1.length * dip2px(MARGIN);
        mPaint.setColor(res.getColor(lineColor));
        for (int i = 0; i < mPoints1.length; i++) {
            //画横坐标上的数值
            drawText(xRawDatas.get(i), i * dip2px(MARGIN) + dip2px(START_MARGIN - 6), canvasHeight - dip2px(START_MARGIN - 6), canvas);
            //画横坐标上的点
            canvas.drawCircle(i * dip2px(MARGIN) + dip2px(START_MARGIN), canvasHeight - dip2px(20), RADIUS, mPaint);
        }
    }

    /**
     * 画上折线
     *
     * @param canvas
     */
    private void drawUpLine(Canvas canvas) {
        Point startp = new Point();
        Point endp = new Point();
        for (int i = 0; i < mPoints1.length - 1; i++) {
            startp = mPoints1[i];
            endp = mPoints1[i + 1];
            //画折线
            canvas.drawLine(i * dip2px(MARGIN) + dip2px(START_MARGIN), startp.y, (i + 1) * dip2px(MARGIN) + dip2px(START_MARGIN), endp.y, mPaint);
        }

        Point startp2 = new Point();
        for (int i = 0; i < mPoints1.length; i++) {
            startp2 = mPoints1[i];
            //画坐标点对应的Y值
            drawText(String.valueOf(yRawData1.get(i)), i * dip2px(MARGIN), startp2.y - dip2px(START_MARGIN + 2), canvas);
        }

    }

    /**
     * 画下折线
     *
     * @param canvas
     */
    private void drawDownLine(Canvas canvas) {
        Point startp = new Point();
        Point endp = new Point();
        for (int i = 0; i < mPoints2.length - 1; i++) {
            startp = mPoints2[i];
            endp = mPoints2[i + 1];
            //画折线
            canvas.drawLine(i * dip2px(MARGIN) + dip2px(START_MARGIN), startp.y, (i + 1) * dip2px(MARGIN) + dip2px(START_MARGIN), endp.y, mPaint);
        }

        Point startp2 = new Point();
        for (int i = 0; i < mPoints2.length; i++) {
            startp2 = mPoints2[i];
            //画坐标点对应的Y值
            drawText(String.valueOf(yRawData2.get(i)), i * dip2px(MARGIN), startp2.y + dip2px(START_MARGIN * 2), canvas);
        }

    }

    private void drawText(String text, int x, int y, Canvas canvas) {
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setTextSize(dip2px(12));
        p.setColor(res.getColor(lineColor));
        p.setTextAlign(Paint.Align.LEFT);
        canvas.drawText(text, x, y, p);
    }

    private void drawBitmap(int resId, float x, float y, Canvas canvas, Paint paint) {
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), resId);
        canvas.drawBitmap(bmp, x, y, paint);
    }

    /**
     * 获得上折线上的所有坐标点
     *
     * @return
     */
    private Point[] getPoints1() {
        Point[] points = new Point[xRawDatas.size()];
        for (int i = 0; i < xRawDatas.size(); i++) {
            int x = i * dip2px(MARGIN) + dip2px(START_MARGIN);
            int y = canvasHeight - (int) (canvasHeight * (yRawData1.get(i) / maxValue)) - dip2px(50);
            points[i] = new Point(x, y);
        }
        return points;
    }

    /**
     * 获得下折线上的所有坐标点
     *
     * @return
     */
    private Point[] getPoints2() {
        Point[] points = new Point[xRawDatas.size()];
        for (int i = 0; i < xRawDatas.size(); i++) {
            int x = i * dip2px(MARGIN) + dip2px(START_MARGIN);
            int y = canvasHeight - (int) (canvasHeight * (yRawData2.get(i) / maxValue)) - dip2px(20);
            points[i] = new Point(x, y);
        }
        return points;
    }

    public void setData(List<Integer> yRawData1, List<Integer> yRawData2, List<String> xRawData, double maxValue) {
        this.maxValue = maxValue;
        this.mPoints1 = new Point[yRawData1.size()];
        this.mPoints2 = new Point[yRawData2.size()];
        this.xRawDatas = xRawData;
        this.yRawData1 = yRawData1;
        this.yRawData2 = yRawData2;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private int dip2px(float dpValue) {
        return (int) (dpValue * dm.density + 0.5f);
    }

    //当前x坐标
    private int x;
    //偏移量
    private int px;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                x = (int) event.getRawX();
                break;
            case MotionEvent.ACTION_MOVE:
                px = (int) event.getRawX() - x;
                x = (int) event.getRawX();
                int curX = getScrollX();
                if (curX - px > tatolWidth - canvasWidth / 2) {
                    px = 0;
                } else if (curX - px < 0 - canvasWidth / 2) {
                    px = 0;
                } else {
                    scrollBy(-px, 0);
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return true;
    }

    public void scrollTMiddle() {
        //scrollTo(tatolWidth / 2, 0);
        scrollTo(0, 0);
    }

}
